<?php
require( 'php/database.php' );
require( 'config.php' );

//
session_start( );

if ( isset( $_SESSION[ 'language_id' ] ) == false )
    $_SESSION[ 'language_id' ] = 1;


//
$aItems = array( );

if ( isset( $_GET[ 'item_count' ] ) == true )
    $iItemCount = $_GET[ 'item_count' ];
else
    $iItemCount = 0;

for ( $i = 0; $i < $iItemCount ; $i++ ) {
    $sItemId = 'item_' . $i;

    if ( isset( $_GET[ $sItemId ] ) == true )
        $iProductId = $_GET[ $sItemId ];
    else
        $iProductId = 0;

    $sItemId = 'item_' . $i . '_quantity';

    if ( isset( $_GET[ $sItemId ] ) == true )
        $iProductQuantity = $_GET[ $sItemId ];
    else
        $iProductQuantity = 1;

    if ( $iProductId != 0 )
        $aItems[ $iProductId ] = $iProductQuantity;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
    <title>
      Report
    </title>
    <link rel="stylesheet" type="text/css" href="css/report.css">
  </head>
  <body>
<?php
if ( count( $aItems ) > 0 ) {
    // Performing SQL query
    $sSQL  = "SELECT p.*, m1.message_text AS product_text, m2.message_text AS product_description";
    $sSQL .= "  FROM products p, messages m1, messages m2";
    $sSQL .= " WHERE p.product_id = m1.product_id AND p.product_id = m2.product_id";
    $sSQL .= "   AND m1.message_type = 0 AND m2.message_type = 1";
    $sSQL .= "   AND m1.language_id = ${_SESSION[ 'language_id' ]} AND m2.language_id = ${_SESSION[ 'language_id' ]}";
    $sSQL .= "   AND p.product_id IN ( " . implode( ',', array_keys( $aItems ) ) . " )";

    //
    $oDB = new Database( $g_sMySqlServer,
                         $g_sMySqlUsername,
                         $g_sMySqlPassword,
                         $g_sMySqlDatabase );
    
    $aRows = $oDB->fetch( $sSQL );
?>
    <table width="100%" cellspacing="0">
      <tr class="report_header">
        <th class="report_header">&nbsp;</th>
      </tr>
<?php
    //
    $iTotalQuantity = 0;
    $fTotalPrice = 0.0;

    //
    $i = 0;

    foreach ( $aRows as $aRow ) {
?>
      <tr>
        <td class="report_item">
          <table width="100%" cellspacing="0">
            <tr class="report_item_top">
              <td width="10%" align="center"><?php echo $aRow[ 'product_code' ]; ?></td>
              <td align="left"><?php echo $aRow[ 'product_text' ]; ?></td>
              <td width="10%" align="right"><?php echo $aRow[ 'product_price' ]; ?></td>
            </tr>
            <tr class="report_item_middle">
              <td width="10%" align="center">&nbsp;</TD>
              <td width="100%" align="left" colspan="2"><?php echo $aRow[ 'product_description' ]; ?></td>
            </tr>
            <tr class="report_item_bottom">
              <td width="10%" align="center">&nbsp;</TD>
              <td align="left"><?php echo $aItems[ $aRow[ 'product_id' ] ]; ?></td>
              <td width="10%" align="right"><?php echo $aItems[ $aRow[ 'product_id' ] ] * $aRow[ 'product_price' ]; ?></td>
            </tr>
          </table>
        </td>
      </tr>
<?php
        $iTotalQuantity += $aItems[ $aRow[ 'product_id' ] ];
        $fTotalPrice    += $aItems[ $aRow[ 'product_id' ] ] * $aRow[ 'product_price' ];

        $i += 1;
    }

    if ( $fTotalPrice != 0.0 ) {
?>
      <tr class="report_footer">
        <th class="report_footer"><?php echo $fTotalPrice; ?></th>
      </tr>
<?php
    }
?>
    </table>
<?php
} else {
?>
    <h1>No items selected.</h1>
<?php
}
?>
  </body>
</html>
